import M from './name.js';
const I = '[-\\+]?\\d+%?',
  E = '[-\\+]?\\d*\\.\\d+%?',
  p = '(?:' + E + ')|(?:' + I + ')',
  x = '[\\s|\\(]+(' + p + ')[,|\\s]+(' + p + ')[,|\\s]+(' + p + ')\\s*\\)?',
  A =
    '[\\s|\\(]+(' +
    p +
    ')[,|\\s]+(' +
    p +
    ')[,|\\s]+(' +
    p +
    ')[,|\\s]+(' +
    p +
    ')\\s*\\)?',
  _ = {
    CSS_UNIT: new RegExp(p),
    rgb: new RegExp('rgb' + x),
    rgba: new RegExp('rgba' + A),
    hsl: new RegExp('hsl' + x),
    hsla: new RegExp('hsla' + A),
    hsv: new RegExp('hsv' + x),
    hsva: new RegExp('hsva' + A),
    hex3: /^#?([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/,
    hex6: /^#?([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/,
    hex4: /^#?([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/,
    hex8: /^#?([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/,
  };
function h(t, r) {
  if ((t || (t = ''), r || (r = {}), t instanceof h)) return t;
  if (!(this instanceof h)) return new h(t, r);
  let e = N(t);
  (this._originalInput = t),
    (this._r = e.r),
    (this._g = e.g),
    (this._b = e.b),
    (this._a = e.a),
    (this._roundA = Math.round(100 * this._a) / 100),
    (this._format = r.format || e.format),
    (this._gradientType = r.gradientType),
    this._r < 1 && (this._r = Math.round(this._r)),
    this._g < 1 && (this._g = Math.round(this._g)),
    this._b < 1 && (this._b = Math.round(this._b)),
    (this._ok = e.ok);
}
h.prototype = {
  isDark() {
    return this.getBrightness() < 128;
  },
  isLight() {
    return !this.isDark();
  },
  isValid() {
    return this._ok;
  },
  getOriginalInput() {
    return this._originalInput;
  },
  getFormat() {
    return this._format;
  },
  getAlpha() {
    return this._a;
  },
  getBrightness() {
    let t = this.toRgb();
    return (t.r * 299 + t.g * 587 + t.b * 114) / 1e3;
  },
  getLuminance() {
    let t = this.toRgb(),
      r,
      e,
      i,
      a,
      n,
      s;
    return (
      (r = t.r / 255),
      (e = t.g / 255),
      (i = t.b / 255),
      r <= 0.03928 ? (a = r / 12.92) : (a = Math.pow((r + 0.055) / 1.055, 2.4)),
      e <= 0.03928 ? (n = e / 12.92) : (n = Math.pow((e + 0.055) / 1.055, 2.4)),
      i <= 0.03928 ? (s = i / 12.92) : (s = Math.pow((i + 0.055) / 1.055, 2.4)),
      0.2126 * a + 0.7152 * n + 0.0722 * s
    );
  },
  setAlpha(t) {
    return (
      (this._a = T(t)), (this._roundA = Math.round(100 * this._a) / 100), this
    );
  },
  toHsv() {
    let t = y(this._r, this._g, this._b);
    return {
      h: t.h * 360,
      s: t.s,
      v: t.v,
      a: this._a,
    };
  },
  toHsvString() {
    let t = y(this._r, this._g, this._b),
      r = Math.round(t.h * 360),
      e = Math.round(t.s * 100),
      i = Math.round(t.v * 100);
    return this._a == 1
      ? 'hsv(' + r + ', ' + e + '%, ' + i + '%)'
      : 'hsva(' + r + ', ' + e + '%, ' + i + '%, ' + this._roundA + ')';
  },
  toHsl() {
    let t = S(this._r, this._g, this._b);
    return {
      h: t.h * 360,
      s: t.s,
      l: t.l,
      a: this._a,
    };
  },
  toHslString() {
    let t = S(this._r, this._g, this._b),
      r = Math.round(t.h * 360),
      e = Math.round(t.s * 100),
      i = Math.round(t.l * 100);
    return this._a == 1
      ? 'hsl(' + r + ', ' + e + '%, ' + i + '%)'
      : 'hsla(' + r + ', ' + e + '%, ' + i + '%, ' + this._roundA + ')';
  },
  toHex(t) {
    return R(this._r, this._g, this._b, t);
  },
  toHexString(t) {
    return '#' + this.toHex(t);
  },
  toHex8(t) {
    return B(this._r, this._g, this._b, this._a, t);
  },
  toHex8String(t) {
    return '#' + this.toHex8(t);
  },
  toRgb() {
    return {
      r: Math.round(this._r),
      g: Math.round(this._g),
      b: Math.round(this._b),
      a: this._a,
    };
  },
  toRgbArray() {
    return [
      Math.round(this._r),
      Math.round(this._g),
      Math.round(this._b),
      // this._a,
    ];
  },
  toRgbString() {
    return this._a == 1
      ? 'rgb(' +
          Math.round(this._r) +
          ', ' +
          Math.round(this._g) +
          ', ' +
          Math.round(this._b) +
          ')'
      : 'rgba(' +
          Math.round(this._r) +
          ', ' +
          Math.round(this._g) +
          ', ' +
          Math.round(this._b) +
          ', ' +
          this._roundA +
          ')';
  },
  toPercentageRgb() {
    return {
      r: Math.round(u(this._r, 255) * 100) + '%',
      g: Math.round(u(this._g, 255) * 100) + '%',
      b: Math.round(u(this._b, 255) * 100) + '%',
      a: this._a,
    };
  },
  toPercentageRgbString() {
    return this._a == 1
      ? 'rgb(' +
          Math.round(u(this._r, 255) * 100) +
          '%, ' +
          Math.round(u(this._g, 255) * 100) +
          '%, ' +
          Math.round(u(this._b, 255) * 100) +
          '%)'
      : 'rgba(' +
          Math.round(u(this._r, 255) * 100) +
          '%, ' +
          Math.round(u(this._g, 255) * 100) +
          '%, ' +
          Math.round(u(this._b, 255) * 100) +
          '%, ' +
          this._roundA +
          ')';
  },
  toName() {
    return this._a === 0
      ? 'transparent'
      : this._a < 1
        ? !1
        : J[R(this._r, this._g, this._b, !0)] || !1;
  },
  toFilter(t) {
    let r = '#' + H(this._r, this._g, this._b, this._a),
      e = r,
      i = this._gradientType ? 'GradientType = 1, ' : '';
    if (t) {
      let a = h(t);
      e = '#' + H(a._r, a._g, a._b, a._a);
    }
    return (
      'progid:DXImageTransform.Microsoft.gradient(' +
      i +
      'startColorstr=' +
      r +
      ',endColorstr=' +
      e +
      ')'
    );
  },
  toString(t) {
    let r = !!t;
    t = t || this._format;
    let e = !1,
      i = this._a < 1 && this._a >= 0;
    return !r &&
      i &&
      (t === 'hex' ||
        t === 'hex6' ||
        t === 'hex3' ||
        t === 'hex4' ||
        t === 'hex8' ||
        t === 'name')
      ? t === 'name' && this._a === 0
        ? this.toName()
        : this.toRgbString()
      : (t === 'rgb' && (e = this.toRgbString()),
        t === 'prgb' && (e = this.toPercentageRgbString()),
        (t === 'hex' || t === 'hex6') && (e = this.toHexString()),
        t === 'hex3' && (e = this.toHexString(!0)),
        t === 'hex4' && (e = this.toHex8String(!0)),
        t === 'hex8' && (e = this.toHex8String()),
        t === 'name' && (e = this.toName()),
        t === 'hsl' && (e = this.toHslString()),
        t === 'hsv' && (e = this.toHsvString()),
        e || this.toHexString());
  },
  clone() {
    return h(this.toString());
  },
  _applyModification(t, r) {
    let e = t.apply(null, [this].concat([].slice.call(r)));
    return (
      (this._r = e._r),
      (this._g = e._g),
      (this._b = e._b),
      this.setAlpha(e._a),
      this
    );
  },
  lighten() {
    return this._applyModification(U, arguments);
  },
  brighten() {
    return this._applyModification(D, arguments);
  },
  darken() {
    return this._applyModification(V, arguments);
  },
  desaturate() {
    return this._applyModification(G, arguments);
  },
  saturate() {
    return this._applyModification(z, arguments);
  },
  greyscale() {
    return this._applyModification(L, arguments);
  },
  spin() {
    return this._applyModification($, arguments);
  },
  _applyCombination(t, r) {
    return t.apply(null, [this].concat([].slice.call(r)));
  },
  analogous() {
    return this._applyCombination(X, arguments);
  },
  complement() {
    return this._applyCombination(q, arguments);
  },
  monochromatic() {
    return this._applyCombination(Z, arguments);
  },
  splitcomplement() {
    return this._applyCombination(W, arguments);
  },
  // Disabled until https://github.com/bgrins/TinyColor/issues/254
  // polyad(number) {
  //   return this._applyCombination(polyad, [number]);
  // },
  triad() {
    return this._applyCombination(v, [3]);
  },
  tetrad() {
    return this._applyCombination(v, [4]);
  },
};
h.fromRatio = function (t, r) {
  if (typeof t == 'object') {
    let e = {};
    for (let i in t)
      Object.prototype.hasOwnProperty.call(t, i) &&
        (i === 'a' ? (e[i] = t[i]) : (e[i] = c(t[i])));
    t = e;
  }
  return h(t, r);
};
function N(t) {
  let r = {
      r: 0,
      g: 0,
      b: 0,
    },
    e = 1,
    i = null,
    a = null,
    n = null,
    s = !1,
    l = !1;
  return (
    typeof t == 'string' && (t = tt(t)),
    typeof t == 'object' &&
      (d(t.r) && d(t.g) && d(t.b)
        ? ((r = P(t.r, t.g, t.b)),
          (s = !0),
          (l = String(t.r).at(-1) === '%' ? 'prgb' : 'rgb'))
        : d(t.h) && d(t.s) && d(t.v)
          ? ((i = c(t.s)),
            (a = c(t.v)),
            (r = j(t.h, i, a)),
            (s = !0),
            (l = 'hsv'))
          : d(t.h) &&
            d(t.s) &&
            d(t.l) &&
            ((i = c(t.s)),
            (n = c(t.l)),
            (r = O(t.h, i, n)),
            (s = !0),
            (l = 'hsl')),
      Object.prototype.hasOwnProperty.call(t, 'a') && (e = t.a)),
    (e = T(e)),
    {
      ok: s,
      format: t.format || l,
      r: Math.min(255, Math.max(r.r, 0)),
      g: Math.min(255, Math.max(r.g, 0)),
      b: Math.min(255, Math.max(r.b, 0)),
      a: e,
    }
  );
}
function P(t, r, e) {
  return {
    r: u(t, 255) * 255,
    g: u(r, 255) * 255,
    b: u(e, 255) * 255,
  };
}
function S(t, r, e) {
  (t = u(t, 255)), (r = u(r, 255)), (e = u(e, 255));
  let i = Math.max(t, r, e),
    a = Math.min(t, r, e),
    n,
    s,
    l = (i + a) / 2;
  if (i == a) n = s = 0;
  else {
    let g = i - a;
    switch (((s = l > 0.5 ? g / (2 - i - a) : g / (i + a)), i)) {
      case t:
        n = (r - e) / g + (r < e ? 6 : 0);
        break;
      case r:
        n = (e - t) / g + 2;
        break;
      case e:
        n = (t - r) / g + 4;
        break;
    }
    n /= 6;
  }
  return {
    h: n,
    s,
    l,
  };
}
function O(t, r, e) {
  let i, a, n;
  (t = u(t, 360)), (r = u(r, 100)), (e = u(e, 100));
  function s(l, g, o) {
    return (
      o < 0 && (o += 1),
      o > 1 && (o -= 1),
      o < 1 / 6
        ? l + (g - l) * 6 * o
        : o < 1 / 2
          ? g
          : o < 2 / 3
            ? l + (g - l) * (2 / 3 - o) * 6
            : l
    );
  }
  if (r === 0) i = a = n = e;
  else {
    let l = e < 0.5 ? e * (1 + r) : e + r - e * r,
      g = 2 * e - l;
    (i = s(g, l, t + 1 / 3)), (a = s(g, l, t)), (n = s(g, l, t - 1 / 3));
  }
  return {
    r: i * 255,
    g: a * 255,
    b: n * 255,
  };
}
function y(t, r, e) {
  (t = u(t, 255)), (r = u(r, 255)), (e = u(e, 255));
  let i = Math.max(t, r, e),
    a = Math.min(t, r, e),
    n = i - a,
    s = 0,
    l = i === 0 ? 0 : n / i,
    g = i;
  if (i == a) s = 0;
  else {
    switch (i) {
      case t:
        s = (r - e) / n + (r < e ? 6 : 0);
        break;
      case r:
        s = (e - t) / n + 2;
        break;
      case e:
        s = (t - r) / n + 4;
        break;
    }
    s /= 6;
  }
  return {
    h: s,
    s: l,
    v: g,
  };
}
function j(t, r, e) {
  (t = u(t, 360) * 6), (r = u(r, 100)), (e = u(e, 100));
  let i = Math.floor(t),
    a = t - i,
    n = e * (1 - r),
    s = e * (1 - a * r),
    l = e * (1 - (1 - a) * r),
    g = i % 6,
    o = [e, s, n, n, l, e][g],
    C = [l, e, e, s, n, n][g],
    k = [n, n, l, e, e, s][g];
  return {
    r: o * 255,
    g: C * 255,
    b: k * 255,
  };
}
function R(t, r, e, i) {
  let a = [
    b(Math.round(t).toString(16)),
    b(Math.round(r).toString(16)),
    b(Math.round(e).toString(16)),
  ];
  return i &&
    a[0].charAt(0) == a[0].charAt(1) &&
    a[1].charAt(0) == a[1].charAt(1) &&
    a[2].charAt(0) == a[2].charAt(1)
    ? a[0].charAt(0) + a[1].charAt(0) + a[2].charAt(0)
    : a.join('');
}
function B(t, r, e, i, a) {
  let n = [
    b(Math.round(t).toString(16)),
    b(Math.round(r).toString(16)),
    b(Math.round(e).toString(16)),
    b(w(i)),
  ];
  return a &&
    n[0].charAt(0) == n[0].charAt(1) &&
    n[1].charAt(0) == n[1].charAt(1) &&
    n[2].charAt(0) == n[2].charAt(1) &&
    n[3].charAt(0) == n[3].charAt(1)
    ? n[0].charAt(0) + n[1].charAt(0) + n[2].charAt(0) + n[3].charAt(0)
    : n.join('');
}
function H(t, r, e, i) {
  return [
    b(w(i)),
    b(Math.round(t).toString(16)),
    b(Math.round(r).toString(16)),
    b(Math.round(e).toString(16)),
  ].join('');
}
h.equals = function (t, r) {
  return !t || !r ? !1 : h(t).toRgbString() == h(r).toRgbString();
};
h.random = function () {
  return h.fromRatio({
    r: Math.random(),
    g: Math.random(),
    b: Math.random(),
  });
};
function G(t, r) {
  r = r === 0 ? 0 : r || 10;
  let e = h(t).toHsl();
  return (e.s -= r / 100), (e.s = m(e.s)), h(e);
}
function z(t, r) {
  r = r === 0 ? 0 : r || 10;
  let e = h(t).toHsl();
  return (e.s += r / 100), (e.s = m(e.s)), h(e);
}
function L(t) {
  return h(t).desaturate(100);
}
function U(t, r) {
  r = r === 0 ? 0 : r || 10;
  let e = h(t).toHsl();
  return (e.l += r / 100), (e.l = m(e.l)), h(e);
}
function D(t, r) {
  r = r === 0 ? 0 : r || 10;
  let e = h(t).toRgb();
  return (
    (e.r = Math.max(0, Math.min(255, e.r - Math.round(255 * -(r / 100))))),
    (e.g = Math.max(0, Math.min(255, e.g - Math.round(255 * -(r / 100))))),
    (e.b = Math.max(0, Math.min(255, e.b - Math.round(255 * -(r / 100))))),
    h(e)
  );
}
function V(t, r) {
  r = r === 0 ? 0 : r || 10;
  let e = h(t).toHsl();
  return (e.l -= r / 100), (e.l = m(e.l)), h(e);
}
function $(t, r) {
  let e = h(t).toHsl(),
    i = (e.h + r) % 360;
  return (e.h = i < 0 ? 360 + i : i), h(e);
}
function q(t) {
  let r = h(t).toHsl();
  return (r.h = (r.h + 180) % 360), h(r);
}
function v(t, r) {
  if (isNaN(r) || r <= 0)
    throw new Error('Argument to polyad must be a positive number');
  let e = h(t).toHsl(),
    i = [h(t)],
    a = 360 / r;
  for (let n = 1; n < r; n++)
    i.push(
      h({
        h: (e.h + n * a) % 360,
        s: e.s,
        l: e.l,
      }),
    );
  return i;
}
function W(t) {
  let r = h(t).toHsl(),
    e = r.h;
  return [
    h(t),
    h({
      h: (e + 72) % 360,
      s: r.s,
      l: r.l,
    }),
    h({
      h: (e + 216) % 360,
      s: r.s,
      l: r.l,
    }),
  ];
}
function X(t, r, e) {
  (r = r || 6), (e = e || 30);
  let i = h(t).toHsl(),
    a = 360 / e,
    n = [h(t)];
  for (i.h = (i.h - ((a * r) >> 1) + 720) % 360; --r; )
    (i.h = (i.h + a) % 360), n.push(h(i));
  return n;
}
function Z(t, r) {
  r = r || 6;
  let e = h(t).toHsv(),
    i = e.h,
    a = e.s,
    n = e.v,
    s = [],
    l = 1 / r;
  for (; r--; )
    s.push(
      h({
        h: i,
        s: a,
        v: n,
      }),
    ),
      (n = (n + l) % 1);
  return s;
}
h.mix = function (t, r, e) {
  e = e === 0 ? 0 : e || 50;
  let i = h(t).toRgb(),
    a = h(r).toRgb(),
    n = e / 100,
    s = {
      r: (a.r - i.r) * n + i.r,
      g: (a.g - i.g) * n + i.g,
      b: (a.b - i.b) * n + i.b,
      a: (a.a - i.a) * n + i.a,
    };
  return h(s);
};
h.readability = function (t, r) {
  let e = h(t),
    i = h(r);
  return (
    (Math.max(e.getLuminance(), i.getLuminance()) + 0.05) /
    (Math.min(e.getLuminance(), i.getLuminance()) + 0.05)
  );
};
h.isReadable = function (t, r, e) {
  let i = h.readability(t, r),
    a,
    n;
  switch (((n = !1), (a = et(e)), a.level + a.size)) {
    case 'AAsmall':
    case 'AAAlarge':
      n = i >= 4.5;
      break;
    case 'AAlarge':
      n = i >= 3;
      break;
    case 'AAAsmall':
      n = i >= 7;
      break;
  }
  return n;
};
h.mostReadable = function (t, r, e) {
  let i = null,
    a = 0,
    n,
    s,
    l,
    g;
  (e = e || {}), (s = e.includeFallbackColors), (l = e.level), (g = e.size);
  for (let o = 0; o < r.length; o++)
    (n = h.readability(t, r[o])), n > a && ((a = n), (i = h(r[o])));
  return h.isReadable(t, i, {
    level: l,
    size: g,
  }) || !s
    ? i
    : ((e.includeFallbackColors = !1), h.mostReadable(t, ['#fff', '#000'], e));
};
h.names = M;
let J = (h.hexNames = K(M));
function K(t) {
  let r = {};
  for (let e in t) Object.prototype.hasOwnProperty.call(t, e) && (r[t[e]] = e);
  return r;
}
function T(t) {
  return (t = parseFloat(t)), (isNaN(t) || t < 0 || t > 1) && (t = 1), t;
}
function u(t, r) {
  Q(t) && (t = '100%');
  let e = Y(t);
  return (
    (t = Math.min(r, Math.max(0, parseFloat(t)))),
    e && (t = parseInt(t * r, 10) / 100),
    Math.abs(t - r) < 1e-6 ? 1 : (t % r) / parseFloat(r)
  );
}
function m(t) {
  return Math.min(1, Math.max(0, t));
}
function f(t) {
  return parseInt(t, 16);
}
function Q(t) {
  return typeof t == 'string' && t.indexOf('.') != -1 && parseFloat(t) === 1;
}
function Y(t) {
  return typeof t == 'string' && t.indexOf('%') != -1;
}
function b(t) {
  return t.length == 1 ? '0' + t : '' + t;
}
function c(t) {
  return t <= 1 && (t = t * 100 + '%'), t;
}
function w(t) {
  return Math.round(parseFloat(t) * 255).toString(16);
}
function F(t) {
  return f(t) / 255;
}
function d(t) {
  return !!_.CSS_UNIT.exec(t);
}
function tt(t) {
  t = t.trim().toLowerCase();
  let r = !1;
  if (M[t]) (t = M[t]), (r = !0);
  else if (t == 'transparent')
    return {
      r: 0,
      g: 0,
      b: 0,
      a: 0,
      format: 'name',
    };
  let e;
  return (e = _.rgb.exec(t))
    ? {
        r: e[1],
        g: e[2],
        b: e[3],
      }
    : (e = _.rgba.exec(t))
      ? {
          r: e[1],
          g: e[2],
          b: e[3],
          a: e[4],
        }
      : (e = _.hsl.exec(t))
        ? {
            h: e[1],
            s: e[2],
            l: e[3],
          }
        : (e = _.hsla.exec(t))
          ? {
              h: e[1],
              s: e[2],
              l: e[3],
              a: e[4],
            }
          : (e = _.hsv.exec(t))
            ? {
                h: e[1],
                s: e[2],
                v: e[3],
              }
            : (e = _.hsva.exec(t))
              ? {
                  h: e[1],
                  s: e[2],
                  v: e[3],
                  a: e[4],
                }
              : (e = _.hex8.exec(t))
                ? {
                    r: f(e[1]),
                    g: f(e[2]),
                    b: f(e[3]),
                    a: F(e[4]),
                    format: r ? 'name' : 'hex8',
                  }
                : (e = _.hex6.exec(t))
                  ? {
                      r: f(e[1]),
                      g: f(e[2]),
                      b: f(e[3]),
                      format: r ? 'name' : 'hex',
                    }
                  : (e = _.hex4.exec(t))
                    ? {
                        r: f(e[1] + '' + e[1]),
                        g: f(e[2] + '' + e[2]),
                        b: f(e[3] + '' + e[3]),
                        a: F(e[4] + '' + e[4]),
                        format: r ? 'name' : 'hex8',
                      }
                    : (e = _.hex3.exec(t))
                      ? {
                          r: f(e[1] + '' + e[1]),
                          g: f(e[2] + '' + e[2]),
                          b: f(e[3] + '' + e[3]),
                          format: r ? 'name' : 'hex',
                        }
                      : !1;
}
function et(t) {
  let r, e;
  return (
    (t = t || {
      level: 'AA',
      size: 'small',
    }),
    (r = (t.level || 'AA').toUpperCase()),
    (e = (t.size || 'small').toLowerCase()),
    r !== 'AA' && r !== 'AAA' && (r = 'AA'),
    e !== 'small' && e !== 'large' && (e = 'small'),
    {
      level: r,
      size: e,
    }
  );
}
export {
  u as bound01,
  T as boundAlpha,
  m as clamp01,
  w as convertDecimalToHex,
  F as convertHexToDecimal,
  c as convertToPercentage,
  h as default,
  K as flip,
  O as hslToRgb,
  j as hsvToRgb,
  N as inputToRGB,
  Q as isOnePointZero,
  Y as isPercentage,
  d as isValidCSSUnit,
  b as pad2,
  f as parseIntFromHex,
  v as polyad,
  R as rgbToHex,
  S as rgbToHsl,
  y as rgbToHsv,
  P as rgbToRgb,
  H as rgbaToArgbHex,
  B as rgbaToHex,
  tt as stringInputToObject,
  et as validateWCAG2Parms,
};
